<?php
// $Id: fhreqchart.manage.inc,v 1.1 2009-03-08 13:25:49 frank Exp $
/**
 * @file
 * Manages the list of requests.
 *
 * Copyright 2009 Frank Heinley.
 *
 * This file is part of fhreqchart.
 *
 * fhreqchart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * fhreqchart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with fhreqchart.  If not, see <http://www.gnu.org/licenses/>.
 */
 
require_once('fhreqchart.module.inc');

/**
 * Manage the list of requests.
 *
 * Function called by the menu to manage the list of requests.
 */
function fhreqchart_manage_requests() {
  // Number of entries on a page
  define('R_PAGER_SIZE', 20);
  $filter = fhreqchart_get_filter();
  $where = " WHERE reqdate = " . $filter['reqdate'];
  fhreqchart_add_css();
  $months = fhreqchart_month_list();
  $query =
    "SELECT art.artist, ttl.title, wrd.ward, reqdate, gnr.genre, r.reqid ".
      _fhreqchart_get_join() .
      $where .
      " ORDER BY reqdate, reqid ";
  $res = pager_query($query, R_PAGER_SIZE);
  $output[] = drupal_get_form('fhreqchart_form_manage_filter');
  $output[] = drupal_get_form('fhreqchart_form_manage_requests', $res);
  if ($pager = theme('pager', NULL, R_PAGER_SIZE)) {
    $output[] = $pager;
  }
  return implode("\n", $output);
}

/**
 * FAPI definition for the form to manage requests.
 *
 * @param &$form_state
 *  Standard form state variable.
 * @param $listres
 *  SQL result resource for the list of requests to edit
 * @return
 *  Array describing the form.
 * @ingroup forms
 */
function fhreqchart_form_manage_requests(&$form_state, $listres) {
  $months = fhreqchart_month_list();
  $years = fhreqchart_year_list();

  $form['req']['#tree'] = TRUE;
  $form['req']['#theme'] = 'fhreqchart_form_manage_requests_list';
  while ($obj = db_fetch_object($listres)) {
    $key = $obj->reqid;
    $form['req'][$key]['reqid'] = array('#type' => 'value', '#value' => $key);
    $form['req'][$key]['year'] = array(
      '#type' => 'select',
      '#options' => $years,
      '#default_value' => date('Y', $obj->reqdate),
    );
    $form['req'][$key]['month'] = array(
      '#type' => 'select',
      '#options' => $months,
      '#default_value' => date('n', $obj->reqdate),
    );
    $form['req'][$key]['title'] = array(
      '#type' => 'textfield',
      '#size' => 20,
      '#default_value' => $obj->title,
      '#autocomplete_path' => 'fhreqchart/titleac',
    );
    $form['req'][$key]['artist'] = array(
      '#type' => 'textfield',
      '#size' => 20,
      '#default_value' => $obj->artist,
      '#autocomplete_path' => 'fhreqchart/artistac',
    );
    $form['req'][$key]['genre'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => $obj->genre,
      '#autocomplete_path' => 'fhreqchart/genreac',
    );
    $form['req'][$key]['ward'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => $obj->ward,
      '#autocomplete_path' => 'fhreqchart/wardac',
    );
    $form['req'][$key]['remove'] = array(
      '#type' => 'checkbox',
      '#default_value' => 0,
    );
  }
  $form['submit'] = array(
    '#type' => submit,
    '#value' => t('Save changes'),
  );
  $form['submitnote'] = array(
    '#value' => t('Remember to save any changes before changing page.'),
  );

  return $form;
}

/**
 * Submit the manage requests form.
 */
function fhreqchart_form_manage_requests_submit(&$form, &$form_state) {
  $delcount = 0;
  foreach ($form_state['values']['req'] as $reqid => $req) {
    if ($req['remove']) {
      db_query("DELETE FROM {fhreqchart} WHERE reqid = %d", $reqid);
      if (db_affected_rows()) {
        $delcount++;
      }
    }
    else {
      fhreqchart_store_request($req);
    }
  }
  $tmp = ($delcount) ? format_plural($delcount, 'One request deleted.', '@count requests deleted.') : '';
  drupal_set_message(t('Changes saved. ') . $tmp);
}

/**
 * FAPI definition for the sub-form to filter the requests.
 *
 * @ingroup forms
 */
function fhreqchart_form_manage_filter(&$form_state) {
  $months = fhreqchart_month_list();
  $years = fhreqchart_year_list();
  $filter = fhreqchart_get_filter();
  $form['#theme'] = 'fhreqchart_manage_filter';

  $form['caption'] = array(
    '#value' => t('Filter by:'),
  );

  $form['month'] = array(
    '#type' => 'select',
    '#options' => $months,
    '#default_value' => $filter['month'],
  );

  $form['year'] = array(
    '#type' => 'select',
    '#options' => $years,
    '#default_value' => $filter['year'],
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
  );
  return $form;
}

/**
 * Submit the filter sub form.
 */
function fhreqchart_form_manage_filter_submit(&$form, &$form_state) {
  $filter['month'] = $form_state['values']['month'];
  $filter['year'] = $form_state['values']['year'];
  fhreqchart_set_filter($filter);
}

/**
 * Set the filter for the requests.
 */
function fhreqchart_set_filter($filter) {
  $filter['reqdate'] = mktime(0, 0, 0, $filter['month'], 1, $filter['year']);
  $_SESSION['fhreqchart_filter'] = serialize($filter);
}

/**
 * Return the filter for the requests.
 */
function fhreqchart_get_filter() {
  if (!isset($_SESSION['fhreqchart_filter'])) {
    $filter = array( 'month' => date('n'), 'year' => date('Y') );
    fhreqchart_set_filter($filter);
  }
  return unserialize($_SESSION['fhreqchart_filter']);
}

/**
 * Format the request filter
 *
 * @ingroup themeable
 */
function theme_fhreqchart_manage_filter(&$form) {
  $row[] = drupal_render($form['caption']);
  $row[] = drupal_render($form['month']);
  $row[] = drupal_render($form['year']);
  $row[] = drupal_render($form['submit']);
  $output[] = theme('table', NULL, array($row), array('class' => 'fhreqchart-filter'));
  $output[] = drupal_render($form);
  return implode("\n", $output);
}

/**
 * Format the list of requests.
 *
 * @ingroup themeable
 */
function theme_fhreqchart_form_manage_requests_list(&$form) {
  $header = array(t('Year'), t('Month'), t('Title'), t('Artist'), t('Genre'), t('Ward'), t('Remove'));
  $rows = array();
  foreach (element_children($form) as $key) {
    $row = array();
    $row[] = drupal_render($form[$key]['year']);
    $row[] = drupal_render($form[$key]['month']);
    $row[] = drupal_render($form[$key]['title']);
    $row[] = drupal_render($form[$key]['artist']);
    $row[] = drupal_render($form[$key]['genre']);
    $row[] = drupal_render($form[$key]['ward']);
    $row[] = array('data' => drupal_render($form[$key]['remove']), 'align' => 'center');
    $rows[] = $row;
  }
  return theme('table', $header, $rows);
}
